package com.csu.tingtian.service.impl;

import com.csu.tingtian.common.CommonResponse;
import com.csu.tingtian.entity.LegalFarm;
import com.csu.tingtian.entity.UnDeterminedFarms;
import com.csu.tingtian.entity.UnLegalFarm;
import com.csu.tingtian.persistence.LegalFarmMapper;
import com.csu.tingtian.persistence.UnDeterminedFarmsMapper;
import com.csu.tingtian.persistence.UnLegalFarmMapper;
import com.csu.tingtian.service.AdminService;
import com.csu.tingtian.service.FarmRentCommentService;
import com.csu.tingtian.service.ProduceCommentService;
import com.csu.tingtian.vo.FarmAndLevel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("adminService")
public class AdminServiceImpl implements AdminService {
    @Autowired
    private UnDeterminedFarmsMapper unDeterminedFarmsMapper;
    @Autowired
    private LegalFarmMapper legalFarmMapper;
    @Autowired
    private UnLegalFarmMapper unLegalFarmMapper;
    @Autowired
    private ProduceCommentService produceCommentService;
    @Autowired
    private FarmRentCommentService farmRentCommentService;

    private LegalFarm legalFarm = null;
    private UnLegalFarm unLegalFarm = null;

    //获取所有申请成为商家的信息
    @Override
    public CommonResponse<List<UnDeterminedFarms>> getAllUnDeterminedFarms() {
        List<UnDeterminedFarms> list = unDeterminedFarmsMapper.selectList(null);
        return CommonResponse.createForSuccess(list);
    }

    //插入一个商家到legalfarm，并删除unDeterminedFarm中的数据
    @Override
    public CommonResponse<LegalFarm> insertLegalFarm(UnDeterminedFarms unDeterminedFarms) {
        legalFarm = new LegalFarm();

        legalFarm.setFarmName(unDeterminedFarms.getFarmName());
        legalFarm.setUserName(unDeterminedFarms.getUserName());
        legalFarm.setFarmCertificateNum(unDeterminedFarms.getFarmCertificateNum());
        legalFarm.setFarmAddress(unDeterminedFarms.getFarmAddress());
        legalFarm.setFarmTotalArea(unDeterminedFarms.getFarmTotalArea());
        legalFarm.setFarmFreeArea(unDeterminedFarms.getFarmFreeArea());

        legalFarmMapper.insert(legalFarm);
        unDeterminedFarmsMapper.deleteById(legalFarm.getUserName());

        return CommonResponse.createForSuccess("插入成功",legalFarm);
    }

    //插入一个不合格的商家到unlegalfarm
    @Override
    public CommonResponse<UnLegalFarm> insertUnLegalFarm(UnDeterminedFarms unDeterminedFarms,String decription) {
        unLegalFarm = new UnLegalFarm();
        unLegalFarm.setFarmName(unDeterminedFarms.getFarmName());
        unLegalFarm.setUserName(unDeterminedFarms.getUserName());
        unLegalFarm.setDescription(decription);
        unDeterminedFarmsMapper.deleteById(unLegalFarm.getUserName());
        unLegalFarmMapper.insert(unLegalFarm);
        return CommonResponse.createForSuccess(unLegalFarm);
    }

    @Override
    public CommonResponse<List<FarmAndLevel>> getLevelUnder() {
        List<FarmAndLevel> farmAndLevelList = new ArrayList<>();
        //首先获取所有商家信息
        List<LegalFarm> legalFarmList = legalFarmMapper.selectList(null);
        //根据商家信息中的农场名，获取该农场的农产品的平均分
        for (int i = 0; i < legalFarmList.size(); i++) {
            int star = produceCommentService.averageStarLevel(legalFarmList.get(i).getFarmName());
            int star1 = farmRentCommentService.averageStarLevel(legalFarmList.get(i).getFarmName());

            FarmAndLevel farmAndLevel = new FarmAndLevel();
            farmAndLevel.setFarmName(legalFarmList.get(i).getFarmName());
            farmAndLevel.setFarmAddress(legalFarmList.get(i).getFarmAddress());
            farmAndLevel.setFarmCertificateNum(legalFarmList.get(i).getFarmCertificateNum());
            farmAndLevel.setUserName(legalFarmList.get(i).getUserName());
            farmAndLevel.setFarmFreeArea(legalFarmList.get(i).getFarmFreeArea());
            farmAndLevel.setFarmTotalArea(legalFarmList.get(i).getFarmTotalArea());

            //计算
            int average = (star + star1) / 2;
            farmAndLevel.setStarLevel(average);
            if(average <=2 )
                farmAndLevelList.add(farmAndLevel);
        }

        if(farmAndLevelList.size() > 0)
            return CommonResponse.createForSuccess(farmAndLevelList);
        else
            return CommonResponse.createForError("不存在2星以下的商家");
    }
}
